package cn.edu.ujn.park.controller;

import cn.edu.ujn.park.dao.User;
import cn.edu.ujn.park.service.IUserService;
import cn.edu.ujn.util.Page;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

@Controller
public class UserController {

    @Autowired
    private IUserService userService;

    @GetMapping("/login")
    public String login_jsp(){          //用于跳转到登录界面
        return "login";
    }

    /**
     * 登陆
     * @param user
     * @param model
     * @param request
     * @return
     */
    @PostMapping("/login")
    public String login(User user, Model model, HttpServletRequest request){
        User user1 = userService.checkLogin(user);
        if(user1 !=null){
            HttpSession session = request.getSession();
            session.setAttribute("USER_SESSION",user1);
            session.setAttribute("userId",user1.getUserId());
            String userType = user1.getUserType();
            if(userType.equals("管理员")){
                return "adminUserInfo";
            }

            return "userSelfInfo";


        } else{
            model.addAttribute("msg","用户名或密码错误");
            return "login";
        }


    }

    /**
     * 分页查询
     * @param model
     * @param page
     * @param rows
     * @param user
     * @return
     */
    @GetMapping("/user")
    public String list(Model model, @RequestParam(value = "page",defaultValue = "1")Integer page ,
                       @RequestParam(value = "row",defaultValue = "10") Integer rows,@ModelAttribute User user){
        user.setStart((page-1)*rows);
        user.setRows(rows);
        if("".equals(user.getUsername())){
            user.setUsername(null);
        }
        if("".equals(user.getPassword())){
            user.setPassword(null);
        }
        if("".equals(user.getPhone())){
            user.setPhone(null);
        }
        if("".equals(user.getUserType())){
            user.setUserType(null);
        }
        Page<User> page1 = userService.selectAll(user);
        page1.setPage(page);
        model.addAttribute("page",page1);
        return "adminUserInfo";
    }
    @GetMapping("/userself")
    public String list2(Model model, @RequestParam(value = "page",defaultValue = "1")Integer page ,
                       @RequestParam(value = "row",defaultValue = "10") Integer rows,@ModelAttribute User user,HttpServletRequest request){
        user.setStart((page-1)*rows);
        user.setRows(rows);
        Integer userId = (Integer) request.getSession().getAttribute("userId");
        user.setUserId(userId);
        Page<User> page1 = userService.selectAll(user);
        page1.setPage(page);
        model.addAttribute("page",page1);
        return "userSelfInfo";
    }
    @PostMapping("/user")
    @ResponseBody
    public String create(User user){

        userService.insert(user);

        return "OK";
    }

    /**
     * 根据id 获取User对象
     * @param id
     * @return
     */
    @GetMapping("/user/{id}")
    @ResponseBody
    public User editUser(@PathVariable("id") Integer id){
        User user = userService.getUserById(id);
        return user;
    }

    /**
     * 更新user
     * @param user
     * @return
     */
    @PutMapping("/user")
    @ResponseBody
    public String updateUser(@RequestBody User user){

        userService.updateByPrimaryKey(user);

        return "OK";
    }

    /**
     * 删除user
     * @param id
     * @return
     */

    @DeleteMapping("/user/{id}")
    @ResponseBody
    public String deleteUser(@PathVariable("id") Integer id){

        userService.deleteByPrimaryKey(id);
        return "OK";
    }
}
